package com.hualan.sfzy.controller;

import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.model.entity.CourseVideo;
import com.hualan.sfzy.service.CourseVideoService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseVideo")
public class CourseVideoController {
    @Autowired
    private CourseVideoService courseVideoService;
    @PostMapping
    public Result save(@RequestBody CourseVideo courseVideo) {
        int i=courseVideoService.add(courseVideo);
        if(i>0)
        {
            return Result.success();
        }
        return Result.error("添加错误");
    }
    @PutMapping
    public Result update(@RequestBody CourseVideo courseVideo) {
        int i=courseVideoService.edit(courseVideo);
        if(i>0){
            return Result.success();
        }
        return Result.error("修改失败");
    }
    @DeleteMapping("{id}")
    public Result delete(@PathVariable int id) {
        int i=courseVideoService.remove(id);
        if(i>0){
            return Result.success();
        }
        return Result.error("删除失败");
    }
    @DeleteMapping
    public Result delete(@RequestParam Integer[] ids) {
        int i=courseVideoService.removeBatch(ids);
        if(i>0){
            return Result.success();
        }
        return Result.error("删除失败");
    }
    @GetMapping("/all")
    public Result getAll(@RequestParam(value = "pageNum", defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                         @RequestParam(value = "pageSize", defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                        CourseVideo courseVideo ) {
        Map<String,Object> map=courseVideoService.findPage(pageNum,pageSize,courseVideo);
        return Result.success(map);

    }
//
    @GetMapping
    public Result get(@RequestParam Integer id) {
        CourseVideo courseVideo=courseVideoService.findById(id);
        return Result.success(courseVideo);
    }
//    根据课程id查询该课程下所有的视频
    @GetMapping("/courseid")
    public Result getCourseId(@RequestParam Integer courseid) {
        List<CourseVideo> courseVideo=courseVideoService.findByCourseId(courseid);
        return Result.success(courseVideo);
    }


}
